package com.jlastu.znb.message;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.jlastu.znb.R;
import com.jlastu.znb.message.Message; // 假设这是你的消息实体类

import java.util.List;

public class MessageAdapter extends ArrayAdapter<Message> {
    public MessageAdapter(Context context, List<Message> messages) {
        super(context, 0, messages);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Message message = getItem(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    message.isIncoming() ? R.layout.incoming_message_item : R.layout.outgoing_message_item,
                    parent,
                    false);
        }

        TextView messageTextView = convertView.findViewById(R.id.messageTextView);
        messageTextView.setText(message.getText());

        TextView senderTextView = convertView.findViewById(R.id.senderTextView);

        // 根据消息类型设置发送者名称
        if (message.isIncoming()) {
            senderTextView.setText("Customer Service:");
        } else {
            String currentUsername = UserSession.getInstance().getUsername();
            senderTextView.setText("用户：" + (currentUsername != null ? currentUsername : "匿名"));
        }

        return convertView;
    }
}